package com.crm.providerdept8003.controller;

import com.crm.api.entity.Dept;
import com.crm.providerdept8003.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * create by yangShen
 * 2019/8/16 13:00
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    //服务发现接口，对外暴露的接口
    @Qualifier("discoveryClient")
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/dept/add",method = RequestMethod.POST)
    public boolean addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }

    @RequestMapping(value = "/dept/get/{id}",method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id){
        return deptService.get(id);
    }

    @RequestMapping(value = "/dept/list",method = RequestMethod.GET)
    public List<Dept> list(){
        return deptService.list();
    }


    /**
    * 增加自己服务描述的接口
    */
    @RequestMapping(value = "/dept/discovery" , method = RequestMethod.GET)
    public Object discovery(){
        //获取在eureka-server中暴露的所有应用实例名称
        List<String> services = discoveryClient.getServices();
        System.out.println("*************"+services);
        System.out.println("总共有多少个微服务："+services.size());

        //获取指定应用名称的信息
        List<ServiceInstance> instances = discoveryClient.getInstances("springcloud-dept");
        for (ServiceInstance instance : instances) {
            //获取指定应用名称的具体信息
            System.out.println(instance.getServiceId());     //实例应用的名称：SPRINGCLOUD-DEPT
            System.out.println(instance.getHost());          //实例应用的IP
            System.out.println(instance.getPort());          //实例应用的端口
            System.out.println(instance.getUri());           //实例应用的URI：http://10.1.241.33:8001
        }
        return this.discoveryClient;
    }
}
